package com.cheyun.netsalev3.dao

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.cheyun.netsalev3.bean.Search
import com.cheyun.netsalev3.bean.dm.DmSearch

@Database(entities = [Search::class,DmSearch::class], version = 2, exportSchema = false)
abstract class AppDatabase : RoomDatabase() {
    abstract fun getSearchDao(): SearchDao
    abstract fun getDmSearchDao(): DmSearchDao

    companion object {

        //用于单例实例化
        @Volatile
        private var instance: AppDatabase? = null

        fun getInstance(context: Context): AppDatabase {
            return instance ?: synchronized(this) {
                instance ?: buildDatabase(context).also { instance = it }
            }
        }

        //创建并预填充数据库。有关详细信息，请参阅本文：
        private fun buildDatabase(context: Context): AppDatabase {
            return Room.databaseBuilder(context, AppDatabase::class.java, "app_room").fallbackToDestructiveMigration().build()
        }
    }
}
